package com.lkm.market2_0.m_imple.steelyarddata;

import com.lkm.frame.P;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;

import java.util.ArrayList;
import java.util.List;


class SaleCacheInfo implements Cloneable {


    public float unitPrice;

    private float weight;
    public  float tempWeight;

    public String goodName;

    public String goodsId;


    private String date;


    public boolean isUseThisDate;

    private String address;


    private int AD, zeroAD, offsetZeroAD, kValue;


    private int unitType;


    private boolean isHistory;


    private boolean isSalesJoin;


    private List<OrderEntity.GoodEntity> goodEntities;


    private boolean isSalesReturn;

    public SaleCacheInfo(String goodsId, String goodName, float unitPrice, float weight, String date, boolean isUseThisDate, int unitType, boolean isSalesReturn, boolean isHistory) {
        update(goodsId, goodName, unitPrice, weight, date, isUseThisDate, unitType, isSalesReturn, isHistory);
    }


    public synchronized void update(String goodsId, String goodName, float unitPrice, float weight, String date, boolean isUseThisDate, int unitType, boolean isSalesReturn, boolean isHistory) {
        this.unitPrice = unitPrice;
        this.tempWeight = weight;
        this.goodName = goodName;
        this.goodsId = goodsId;
        this.date = date;
        this.isUseThisDate = isUseThisDate;
        this.unitType = unitType;

        this.isSalesReturn = isSalesReturn;
        this.isHistory = isHistory;
    }

    public float getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(float unitPrice) {
        this.unitPrice = unitPrice;
    }

    public float getWeight() {
        return weight;
    }

    public void upRealWeight(float weight) {
        this.weight = weight;
    }

    public String getGoodName() {
        return goodName;
    }

    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }


    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public int getAD() {
        return AD;
    }

    public void setAD(int AD) {
        this.AD = AD;
    }

    public int getZeroAD() {
        return zeroAD;
    }

    public void setZeroAD(int zeroAD) {
        this.zeroAD = zeroAD;
    }

    public int getOffsetZeroAD() {
        return offsetZeroAD;
    }

    public void setOffsetZeroAD(int offsetZeroAD) {
        this.offsetZeroAD = offsetZeroAD;
    }

    public void setKvalue(int kValue) {
        this.kValue = kValue;
    }

    public int getKvalue() {
        return kValue;
    }


    public int getUnitType() {
        return unitType;
    }


    public boolean isSalesReturn() {
        return isSalesReturn;
    }

    public void setSalesReturn(boolean salesReturn) {
        isSalesReturn = salesReturn;
    }

    public boolean isSalesJoin() {
        return isSalesJoin;
    }

    public void setSalesJoin(boolean salesJoin) {
        isSalesJoin = salesJoin;
    }


    public synchronized List<OrderEntity.GoodEntity> getGoodEntities() {
        return goodEntities;
    }


    public synchronized void addGoodEntitie(OrderEntity.GoodEntity good) {
        //P.d("Bill->addGoodEntities:good:" + (good != null));
        if (goodEntities == null) {
            goodEntities = new ArrayList<>();
        }
        goodEntities.add(good);
        //P.d("Bill->addGoodEntities:good:" + ",size:" + goodEntities.size());
    }

    public synchronized int removeGoodEntitie(int index) {
        if (goodEntities != null) {
            goodEntities.remove(index);
        }
        return goodEntities.size();
    }

    public synchronized void removeAllGoodEntitie() {
        if (goodEntities != null) {
            goodEntities.clear();
        }
    }


    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();

    }

    public static OrderEntity.GoodEntity toGoodEntity(SteelyardInfo steelyardInfo, SaleCacheInfo saveSaleCacheInfo) {


        String checkResult = "";

        if (!StringHelp.isEmpty(saveSaleCacheInfo.getGoodName())) {
            if (!CollectionHelp.isEmpty(steelyardInfo.getBreedTo())) {
                List<BreedTo> breedTos = steelyardInfo.getBreedTo();
                for (BreedTo breedTo : breedTos) {
                    if (saveSaleCacheInfo.getGoodName().equals(breedTo.goods_name)) {
                        saveSaleCacheInfo.setGoodsId("" + breedTo.goods_id);
                        saveSaleCacheInfo.setAddress(breedTo.address);
                        checkResult = breedTo.checkResult;
                    }
                }
            }
        }

        if (StringHelp.isEmpty(saveSaleCacheInfo.getGoodsId())) {
            saveSaleCacheInfo.setGoodsId("0");
        }

        OrderEntity.GoodEntity goodEntity = new OrderEntity.GoodEntity(saveSaleCacheInfo.getGoodsId(), saveSaleCacheInfo.getGoodName(), saveSaleCacheInfo.getUnitPrice(), saveSaleCacheInfo.getWeight(), saveSaleCacheInfo.getAddress(), saveSaleCacheInfo.getAD(), saveSaleCacheInfo.getZeroAD(), saveSaleCacheInfo.getOffsetZeroAD(), saveSaleCacheInfo.getKvalue(), saveSaleCacheInfo.getUnitType(), new OrderEntity.GoodEntityEx());
        goodEntity.checkResult = checkResult;
        return goodEntity;
    }

    public boolean isHistory() {
        return isHistory;
    }
}
